
package gblend.formatcovert;

/**
 *
 * @author cjjin
 */

/*
* Copyright 2010, Center for Advanced Information Systems, Nanyang Technological University
*
* File name: FGSort.java
*
* Abstract: Sort the frequent fragment by size
*
* Current Version:      0.1
* Auther:               Jin Changjiu
* Modified Date:        June.3,2009
*
*/


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */



import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.Vector;
import gblend.frequentindex.Vertex;
public class FGSort {
   
    private static String filename = null;

    public static void main (String[] args) throws FileNotFoundException
    {
         filename = "AIDS10k.fp";
         formatDotFile(filename);
    }



    public static void formatDotFile(String filename)
    {
	String output1=filename+".fpn";
        String strLine = null;
	FileOutputStream outStream1;
	PrintStream p;
        PrintStream q;
	FileInputStream inStream;
	DataInputStream in;
	BufferedReader br;
	File fin=new File(filename);
	File fout1=new File(output1);
       
	if(fin.exists())
	{
            try {

       		outStream1=new FileOutputStream(fout1,false);
                inStream=new FileInputStream(fin);
		// Connect print stream to the output stream
                p = new PrintStream( outStream1 );
               	// Get the object of DataInputStream
                in = new DataInputStream(inStream);
                br = new BufferedReader(new InputStreamReader(in));
		//Read file line by line
    		try {

                      int vn = 0;
                      int en = 0;
                      int f = 0;
                      int graphnum = 0;
                      Vector<String> nodelable = null;
                      Vector<String> edge = null;
                      while ((strLine = br.readLine()) != null)
                      {

                       // Print the content on the console
                          if(strLine.contains("t #"))
                          {
                              String[] strnode = strLine.split("\\s");
                               vn = 0;
                               en = 0;
                               f = Integer.parseInt(strnode[4]);
                               nodelable = new Vector<String>();
                               edge = new Vector<String>();
                           }
                           else if(strLine.contains("v"))
                           {
                               vn++;
                               String[] strnode = strLine.split("\\s");
                               nodelable.addElement(strnode[2]);

                           }
                           else if(strLine.contains("e"))
                           {
                               en++;
                               String[] stredge = strLine.split("\\s");
                               edge.addElement(stredge[1]+" "+stredge[2]);

                           }
                           else
                           {
                                if(en<=15)
                                {
                                    p.println("t # "+(graphnum++)+" * "+f);
                                for(int i = 0; i< nodelable.size();i++)
                                {
                                    Vertex node = new Vertex();
                                    //node.setLabel(nodelable.elementAt(i));
                                    p.println("v "+i+" "+nodelable.elementAt(i));
                                    //tmpgraph.addnode(node);
                                }
                                 for(int i = 0; i< edge.size();i++)
                                 {
                                    String[] strnode = edge.elementAt(i).split("\\s");
                                    int e1 = Integer.parseInt(strnode[0]);
                                    int e2 = Integer.parseInt(strnode[1]);
                                    p.println("e "+e1+" "+e2+" 0");
                                    
                                 }
                               
                                }
                                p.println();
                           }

                       }
            
                	//Close the input stream
                        in.close();
			p.close();

		    }
		    catch (IOException e)
		    {
			// TODO Auto-generated catch block
			e.printStackTrace();
                    }
                    } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
			e.printStackTrace();
		}
            }

	}


}